package top.infopub.mgr.cap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.FinancialInstitutionsDto;
import top.infopub.mgr.cap.dao.FinancialInstitutionsDao;
import top.infopub.mgr.cap.service.FinancialInstitutionsService;
import top.infopub.model.cap.FinancialElectSublist;
import top.infopub.model.cap.FinancialInstitutions;
import top.infopub.model.cap.FinancialInstitutionsDetail;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/23 21:31
 * @Version 1.0  *金融机构选择
 */
@Service
public class FinancialInstitutionsServiceImpl implements FinancialInstitutionsService {
    @Autowired
    private FinancialInstitutionsDao financialInstitutionsDao;

    /* 金融机构选择列表接口*/
    @Override
    public Page<FinancialInstitutions> selectPage(Page<FinancialInstitutions> page, FinancialInstitutions financialInstitutions) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return financialInstitutionsDao.selectPage(financialInstitutions);
    }

    //金融机构选择详情页面 ID 是主表的SGUID
    @Override
    public FinancialInstitutionsDto queryDetailById(String id, String proCode) {
        FinancialInstitutionsDto dto = new FinancialInstitutionsDto();
        ArrayList<FinancialInstitutionsDetail> arrayList = new ArrayList<>();
        /*根据id查询金融机构选择表数据*/
        if (!StringUtils.isEmpty(id)) {
            FinancialInstitutions financialInstitutions = financialInstitutionsDao.findById(id);
            //主表ID
            String financialInstitutionsId = financialInstitutions.getId();
            //根据主表ID查询子表数据
            List<FinancialElectSublist> financialElectSublistList = financialInstitutionsDao.selectSublistById(financialInstitutionsId);
            for (FinancialElectSublist financialElectSublist : financialElectSublistList) {
                //根据子表的金融ID 查询详情数据
                String setupFinancialId = financialElectSublist.getSetupFinancialId();
                FinancialInstitutionsDetail financialInstitutionsDetail = financialInstitutionsDao.selectById(setupFinancialId,financialInstitutions.getProCode());
                arrayList.add(financialInstitutionsDetail);
            }
            dto.setFinancialInstitutions(financialInstitutions);
            dto.setFinancialInstitutionsDetailList(arrayList);
        }
        return dto;
    }

    //根据ID 查询数据
    @Override
    public FinancialInstitutions selectByKey(String id) {
        return financialInstitutionsDao.selectByKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(FinancialInstitutionsDto financing, String proCode) {
        /*主表数据*/
        FinancialInstitutions financialInstitutions = financing.getFinancialInstitutions();
        //vo
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (financialInstitutions != null) {
            //主表是融资批复点提交后写入的数据 所以肯定有数据 就只用修改
            financialInstitutions.setUptBy(vo.getId());
            financialInstitutions.setUptTime(new Date());
            /*根据id修改主表*/
            financialInstitutionsDao.updateById(financialInstitutions);
        }
        List<FinancialInstitutionsDetail> financialInstitutionsDetailList = financing.getFinancialInstitutionsDetailList();
        if (financialInstitutionsDetailList != null) {
            for (FinancialInstitutionsDetail detail : financialInstitutionsDetailList) {
                /*子表的数据是融资批复点提交来的 所以肯定有数据 就只用修改*/
                detail.setUptBy(vo.getId());
                detail.setUptTime(new Date());
                //修改子表数据  这个参数ID是金融机构的SGUID 是详情sql查询的
                financialInstitutionsDao.updateBySublist(detail);
            }
        }
        return 1;
    }
}
